{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dee4e917-41f8-417d-b6d3-92e0cd4d29f7",
   "metadata": {},
   "source": [
    "# 模块导入（面相对象编程）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31885f43-4c93-433f-9a3f-44e9c4ecbb8f",
   "metadata": {},
   "source": [
    "## **内置模块**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "5631b789-ba57-4aad-bb85-92eba9d1c388",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.0\n"
     ]
    }
   ],
   "source": [
    "import math  # Import the math module\n",
    "print(math.sqrt(16))  # Print the square root of 16, which should output 4.0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4700bf3-aedc-4e88-9c87-2dd946d0c177",
   "metadata": {},
   "source": [
    "## **第三方模块**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "58886fc8-56a0-47b6-abdb-e11bc01e9d8e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import requests  # Import the requests module to make HTTP requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "bec8d612-e5ad-43f0-97b8-a588ba7882c4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Send a GET request to the specified URL and store the response\n",
    "response = requests.get(\"https://www.baidu.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a1eb66a2-fd1f-434d-9412-1600d0303058",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    }
   ],
   "source": [
    "# Print the status code of the response, which should be 200\n",
    "print(response.status_code)  # Output: 200"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06073ed0-d3e4-4e55-91e3-fd1affc9706f",
   "metadata": {},
   "source": [
    "## **导入模块中的函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "21b4a844-2344-484f-9719-44990b827ecc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.0\n"
     ]
    }
   ],
   "source": [
    "from math import sqrt\n",
    "print(sqrt(16))  # 输出：4.0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5565ad7-8944-4e89-b031-788656b2580a",
   "metadata": {},
   "source": [
    "## **导入模块中的所有函数**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c1ff598-587e-4759-adea-cd9be6109f4b",
   "metadata": {},
   "source": [
    "不推荐使用 容易引起不必要的命名问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "e919ee50-96bb-49de-a8f4-e2febf7e9344",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.0\n",
      "120\n"
     ]
    }
   ],
   "source": [
    "from math import *\n",
    "print(sqrt(16))  # 输出：4.0\n",
    "print(factorial(5))  # 输出：120"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4dd5bb8-d233-42a1-a529-04e5da682fb7",
   "metadata": {},
   "source": [
    "## **使用as关键字导入模块**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "7db9ae6c-7e88-461e-8f56-979d2daa8866",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "758b5c42-acf0-4941-a9fa-a4270f507ae3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "data = {'Name': ['John', 'Anna', 'Peter'],\n",
    "        'Age': [28, 24, 22]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "88c8e630-0533-47c8-a7e0-d85b626063b1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "06c53a59-39f9-46c1-baa1-30c3b975807c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>John</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Anna</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Peter</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Name  Age\n",
       "0   John   28\n",
       "1   Anna   24\n",
       "2  Peter   22"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8df8a9f-20fb-4156-8752-69c7fb9d0385",
   "metadata": {},
   "source": [
    "## **导入模块中的子模块**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efc5d07c-3397-4fc1-a499-cd08adf72f63",
   "metadata": {},
   "source": [
    "有时，模块中可能还有子模块，我们可以使用from...import语句来导入子模块，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "17bb0ace-9f12-435d-b27d-efdeae50e311",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D:\\jupyterlab\\python基础\n"
     ]
    }
   ],
   "source": [
    "import os.path    # => from os import path\n",
    "print(os.path.abspath('.'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfb0213b-eb79-4f5f-8b4b-05ab2384e0ea",
   "metadata": {},
   "source": [
    "## **系统路径**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fd03fb9-1caa-4745-be32-d3a9f47b5f5d",
   "metadata": {},
   "source": [
    "我们可以通过sys模块来查看和修改Python搜索模块的路径，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "abc86839-21c9-4af3-bada-fef74ea63849",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D:\\\\jupyterlab\\\\python基础', 'C:\\\\Users\\\\666\\\\anaconda3\\\\python311.zip', 'C:\\\\Users\\\\666\\\\anaconda3\\\\DLLs', 'C:\\\\Users\\\\666\\\\anaconda3\\\\Lib', 'C:\\\\Users\\\\666\\\\anaconda3', '', 'C:\\\\Users\\\\666\\\\anaconda3\\\\Lib\\\\site-packages', 'C:\\\\Users\\\\666\\\\anaconda3\\\\Lib\\\\site-packages\\\\win32', 'C:\\\\Users\\\\666\\\\anaconda3\\\\Lib\\\\site-packages\\\\win32\\\\lib', 'C:\\\\Users\\\\666\\\\anaconda3\\\\Lib\\\\site-packages\\\\Pythonwin']\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "print(sys.path)\n",
    "sys.path.append('/path/to/your/module')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa1f809c-081c-4228-9bc5-25f656574893",
   "metadata": {},
   "source": [
    "## **创建和使用自定义模块**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "f9338a77-efb9-4d70-a016-1ddd989dcb57",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import my_model as mymodel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "79c146f3-73ac-41e8-9d41-2017e02dee23",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, 王小二!\n"
     ]
    }
   ],
   "source": [
    "mymodel.greet(\"王小二\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
